using System;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WorkflowActions;

namespace DP.Sharepoint.Workflow
{
    public partial class LookupUserInfo : Activity
    {
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context",
                                                                                         typeof (WorkflowContext),
                                                                                         typeof (LookupUserInfo));

        public static DependencyProperty PropertyValueVariableProperty =
            DependencyProperty.Register("PropertyValueVariable", typeof (string), typeof (LookupUserInfo));


        public static DependencyProperty UserNameProperty = DependencyProperty.Register("UserName", typeof (string),
                                                                                        typeof (LookupUserInfo));


        public static DependencyProperty UserPropertyProperty = DependencyProperty.Register("UserProperty",
                                                                                            typeof (string),
                                                                                            typeof (LookupUserInfo));

        public LookupUserInfo()
        {
            InitializeComponent();
        }

        [Description("Context")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get { return ((WorkflowContext) (base.GetValue(__ContextProperty))); }
            set { base.SetValue(__ContextProperty, value); }
        }

        [Description("UserName")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string UserName
        {
            get { return ((string) (base.GetValue(UserNameProperty))); }
            set { base.SetValue(UserNameProperty, value); }
        }

        [Description("UserProperty")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string UserProperty
        {
            get { return ((string) (base.GetValue(UserPropertyProperty))); }
            set { base.SetValue(UserPropertyProperty, value); }
        }


        [Description("PropertyValueVariable")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string PropertyValueVariable
        {
            get { return ((string) (base.GetValue(PropertyValueVariableProperty))); }
            set { base.SetValue(PropertyValueVariableProperty, value); }
        }


        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                string err = string.Empty;

                SPList userInfo = __Context.Web.SiteUserInfoList;


                SPQuery q = new SPQuery();

                q.Query = "<Where><Eq><FieldRef Name='Name' /><Value Type='Text' >" + UserName + "</Value></Eq></Where>";


                SPListItemCollection items = userInfo.GetItems(q);

                if (items != null && items.Count > 0)
                {
                    SPListItem user = items[0];

                    if (user.Fields.ContainsField(UserProperty))
                        PropertyValueVariable = user[UserProperty].ToString();
                    else
                        err = "Property not found.";
                }
                else
                    err = "User not found.";


                if (!string.IsNullOrEmpty(err))
                    Common.LogExceptionToWorkflowHistory(new ArgumentOutOfRangeException(err), executionContext,
                                                         WorkflowInstanceId);
            }
            catch (Exception e)
            {
                Common.LogExceptionToWorkflowHistory(e, executionContext, WorkflowInstanceId);
            }
            return ActivityExecutionStatus.Closed;
        }
    }
}